package study.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import study.model.User;

public class UserDao {

	public void addUser(User user) throws SQLException, ClassNotFoundException{
		Class.forName("oracle.jdbc.driver.OracleDriver");
		Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@152.149.47.87:8082:edu", "edu", "edu");
		PreparedStatement prepareStatement = connection.prepareStatement("insert into users(id,name,password) values(?,?,?)");
		prepareStatement.setString(1, user.getId());
		prepareStatement.setString(2, user.getName());
		prepareStatement.setString(3, user.getPassword());

		prepareStatement.executeUpdate();

		prepareStatement.close();
		connection.close();
	}

	public User getUser(String id) throws SQLException, ClassNotFoundException{
		Class.forName("oracle.jdbc.driver.OracleDriver");
		Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@152.149.47.87:8082:edu", "edu", "edu");
		PreparedStatement prepareStatement = connection.prepareStatement("select * from users where id = ?");
		prepareStatement.setString(1, id);
		ResultSet resultSet = prepareStatement.executeQuery();
		resultSet.next();
		User user = new User();
		user.setId(resultSet.getString("id"));
		user.setName(resultSet.getString("name"));
		user.setPassword("password");

		prepareStatement.close();
		connection.close();

		return user;
	}

	public static void main(String[] args) throws SQLException, ClassNotFoundException {
		UserDao userDao = new UserDao();
		User user = new User();
		user.setId("haibane84");
		user.setName("Jeado Ko");
		user.setPassword("1111");

		userDao.addUser(user);

		System.out.println(user.getId()+"등록 성공");

		User user2 = userDao.getUser("haibane84");

		System.out.println(user2.getId()+"조회 성공");
	}
}
